﻿// Author: Arjen van Zanten
//
// Version 1.0: Start up
// Version 1.2: (Ammar Abdulamir) Rewritten to use the ImageData class in TLD_Vision library
//
// A processing class that holds static functions for basic Bitmap processing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using TLD_Vision;

namespace TLD_LPR.Lighting
{
    public class BitmapProcessor
    {
        /// <summary>
        /// Thresholds the given sourceImage, with the given threshold.
        /// </summary>
        /// <param name="sourceImage">Input Bitmap</param>
        /// <param name="threshold">Threshold</param>
        public static unsafe void Threshold(Bitmap sourceImage, int threshold)
        {
			TLD_Vision.Image image = new TLD_Vision.Image(sourceImage);
			ImageData data = image.GetImageData();
			uint currentPixel;
			
			for(int px = 0; px < image.Width * image.Height; px++)
			{
				currentPixel = (uint)(data[px].r * 0.11 + data[px].g * 0.59 + data[px].b * 0.3);
				
				if(currentPixel > threshold)
					data[px] = new PixelData { ui = 0xFFFFFFFF };
				else
					data[px] = new PixelData { ui = 0xFF000000 };
			}

			image.Unlock();
			sourceImage = image.GetBitmap();
        }
    }
}
